package poswar.contribute.mbean;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import poswar.contribute.util.FacesUtils;


@ManagedBean
@SessionScoped
public class UserBean {
	private static Pattern userNamePattern = Pattern.compile("[A-Za-z0-9.]+", Pattern.CASE_INSENSITIVE);	
	
	private String email = "Anonymous";
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		if (email.equalsIgnoreCase("INSTRUCTOR") && !FacesUtils.isCallerLocalHost()) {
			email = "fake.instructor";
		} else {
		      Matcher matcher = userNamePattern.matcher(email);
		      if (!matcher.matches()) {
		    	  email = "illegal.email";
		      }
		}

		this.email = email;
	}	

	
	public boolean isInstructor() {
		return email.equalsIgnoreCase("INSTRUCTOR");
	}
}
